import 'package:flutter/material.dart';

/// 日历事件类型枚举
enum CalendarEventType {
  /// 生日事件
  birthday(Icon(Icons.cake), Colors.pink),
  /// 游玩事件
  play(Icon(Icons.sports_kabaddi), Colors.green),
  /// 看病事件
  medical(Icon(Icons.medical_services), Colors.red),
  /// 纪念日事件
  anniversary(Icon(Icons.favorite), Colors.purple),
  /// 家庭聚会事件
  familyGathering(Icon(Icons.people), Colors.orange),
  /// 学习事件
  study(Icon(Icons.school), Colors.blue),
  /// 其他事件
  other(Icon(Icons.event), Colors.grey);

  /// 事件类型对应的图标
  final Icon icon;
  /// 事件类型对应的颜色
  final Color color;

  const CalendarEventType(this.icon, this.color);
}

/// 日历事件模型
class CalendarEvent {
  /// 事件唯一标识符
  final String id;
  /// 事件标题
  final String title;
  /// 事件描述
  final String description;
  /// 事件开始时间
  final DateTime startTime;
  /// 事件结束时间
  final DateTime? endTime;
  /// 事件类型
  final CalendarEventType eventType;
  /// 事件地点
  final String? location;
  /// 事件参与者
  final List<String>? participants;
  /// 是否全天事件
  final bool isAllDay;
  /// 是否重复事件
  final bool isRecurring;
  /// 重复类型（每天、每周、每月、每年）
  final String? recurrenceType;
  /// 提醒时间（事件前多少分钟提醒）
  final int? reminderMinutes;

  /// 构造函数
  CalendarEvent({
    required this.id,
    required this.title,
    required this.description,
    required this.startTime,
    this.endTime,
    required this.eventType,
    this.location,
    this.participants,
    this.isAllDay = false,
    this.isRecurring = false,
    this.recurrenceType,
    this.reminderMinutes,
  });

  /// 从JSON创建事件对象
  factory CalendarEvent.fromJson(Map<String, dynamic> json) {
    return CalendarEvent(
      id: json['id'] as String,
      title: json['title'] as String,
      description: json['description'] as String,
      startTime: DateTime.parse(json['startTime'] as String),
      endTime: json['endTime'] != null ? DateTime.parse(json['endTime'] as String) : null,
      eventType: CalendarEventType.values.firstWhere(
        (e) => e.name == json['eventType'],
        orElse: () => CalendarEventType.other,
      ),
      location: json['location'] as String?,
      participants: json['participants'] != null
          ? List<String>.from(json['participants'] as List)
          : null,
      isAllDay: json['isAllDay'] as bool? ?? false,
      isRecurring: json['isRecurring'] as bool? ?? false,
      recurrenceType: json['recurrenceType'] as String?,
      reminderMinutes: json['reminderMinutes'] as int?,
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'description': description,
      'startTime': startTime.toIso8601String(),
      'endTime': endTime?.toIso8601String(),
      'eventType': eventType.name,
      'location': location,
      'participants': participants,
      'isAllDay': isAllDay,
      'isRecurring': isRecurring,
      'recurrenceType': recurrenceType,
      'reminderMinutes': reminderMinutes,
    };
  }

  /// 创建事件副本并更新指定字段
  CalendarEvent copyWith({
    String? id,
    String? title,
    String? description,
    DateTime? startTime,
    DateTime? endTime,
    CalendarEventType? eventType,
    String? location,
    List<String>? participants,
    bool? isAllDay,
    bool? isRecurring,
    String? recurrenceType,
    int? reminderMinutes,
  }) {
    return CalendarEvent(
      id: id ?? this.id,
      title: title ?? this.title,
      description: description ?? this.description,
      startTime: startTime ?? this.startTime,
      endTime: endTime ?? this.endTime,
      eventType: eventType ?? this.eventType,
      location: location ?? this.location,
      participants: participants ?? this.participants,
      isAllDay: isAllDay ?? this.isAllDay,
      isRecurring: isRecurring ?? this.isRecurring,
      recurrenceType: recurrenceType ?? this.recurrenceType,
      reminderMinutes: reminderMinutes ?? this.reminderMinutes,
    );
  }
}
